package com.bnq.face;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.view.Gravity;
import android.view.SurfaceView;
import android.view.ViewGroup;
import android.widget.FrameLayout;

public class ImageUtils {

    // 获取品牌
    public static String getPhoneBrand(){
        String brand = android.os.Build.BRAND;
        if (brand == null || brand.isEmpty()) {
            return "unknown";
        }
        return brand.toLowerCase();
    }

    // 获取设备名
    public static String getPhoneModel(){
        String model = android.os.Build.MODEL;
        if (model == null || model.isEmpty()) {
            return "unknown";
        }
        return model.toLowerCase();
    }

    // 获取型号
    public static String getPhoneDevice(){
        String device = android.os.Build.DEVICE;
        if (device == null || device.isEmpty()) {
            return "unknown";
        }
        return device.toLowerCase();
    }

    // 获取制造商
    public static  String getPhoneManufacturer(){
        String manufacturer = android.os.Build.MANUFACTURER;
        if (manufacturer == null || manufacturer.isEmpty()) {
            return "unknown";
        }
        return manufacturer.toLowerCase();
    }

    /**
     * 旋转图片
     * @param bitmap
     * @param rotation
     * @Return
     */
    public static Bitmap getRotatedBitmap(Bitmap bitmap, int rotation) {
        Matrix matrix = new Matrix();
        matrix.postRotate(rotation);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                bitmap.getHeight(), matrix, false);
    }

    /**
     * 镜像翻转图片
     * @param bitmap
     * @Return
     */
    public static Bitmap getFlipBitmap(Bitmap bitmap) {
        Matrix matrix = new Matrix();
        matrix.setScale(-1, 1);
        matrix.postTranslate(bitmap.getWidth(), 0);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                bitmap.getHeight(), matrix, false);
    }

    //顺时针 90° 旋转（rotate +90°）
    public static byte[] rotateNV21Clockwise90(byte[] src, int width, int height) {
        int ySize = width * height;
        int uvHeight = height / 2;

        byte[] dst = new byte[src.length];

        // 1. 旋转 Y
        int index = 0;
        for (int x = 0; x < width; x++) {
            for (int y = height - 1; y >= 0; y--) {
                dst[index++] = src[y * width + x];
            }
        }

        // 2. 旋转 UV（每 2 个字节为一组 VU）
        for (int x = 0; x < width; x += 2) {
            for (int y = uvHeight - 1; y >= 0; y--) {
                int srcIndex = ySize + y * width + x;
                dst[index++] = src[srcIndex];     // V
                dst[index++] = src[srcIndex + 1]; // U
            }
        }

        return dst;
    }

    //逆时针 90° 旋转（rotate -90° / 270°）
    public static byte[] rotateNV21CounterClockwise90(byte[] src, int width, int height) {
        int ySize = width * height;
        int uvHeight = height / 2;

        byte[] dst = new byte[src.length];

        // 1. 旋转 Y
        int index = 0;
        for (int x = width - 1; x >= 0; x--) {
            for (int y = 0; y < height; y++) {
                dst[index++] = src[y * width + x];
            }
        }

        // 2. 旋转 UV
        for (int x = width - 2; x >= 0; x -= 2) {
            for (int y = 0; y < uvHeight; y++) {
                int srcIndex = ySize + y * width + x;
                dst[index++] = src[srcIndex];     // V
                dst[index++] = src[srcIndex + 1]; // U
            }
        }

        return dst;
    }

    //NV21 左右镜像（横向翻转）
    public static byte[] mirrorNV21(byte[] src, int width, int height) {
        int ySize = width * height;
        int uvHeight = height / 2;

        byte[] dst = new byte[src.length];

        // 1. 镜像 Y 分量
        for (int y = 0; y < height; y++) {
            int rowStart = y * width;
            for (int x = 0; x < width; x++) {
                dst[rowStart + x] = src[rowStart + (width - 1 - x)];
            }
        }

        // 2. 镜像 UV 分量
        int uvStart = ySize;
        for (int y = 0; y < uvHeight; y++) {
            int rowStart = uvStart + y * width;
            for (int x = 0; x < width; x += 2) {
                int srcIndex = rowStart + (width - 2 - x);
                dst[rowStart + x] = src[srcIndex];       // V
                dst[rowStart + x + 1] = src[srcIndex + 1]; // U
            }
        }

        return dst;
    }


    // 设置 SurfaceView 的大小来模拟 centerCrop
    public static void layoutSurfaceViewCenterCrop(SurfaceView surfaceView, int previewWidth, int previewHeight) {
        ViewGroup parent = (ViewGroup) surfaceView.getParent();
        int viewWidth = parent.getWidth();
        int viewHeight = parent.getHeight();

        float scaleX = (float) viewWidth / previewWidth;
        float scaleY = (float) viewHeight / previewHeight;
        float scale = Math.max(scaleX, scaleY); // centerCrop：按最大比例缩放并裁剪

        int scaledWidth = Math.round(previewWidth * scale);
        int scaledHeight = Math.round(previewHeight * scale);

        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(scaledWidth, scaledHeight);
        params.gravity = Gravity.CENTER;
        surfaceView.setLayoutParams(params);
    }


}